/*
 * UlozeniShowPanel.java
 *
 * Created on 19. �erven 2007, 20:53
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cz.possoft.client.gui.panel.nastavenispodnipanel.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import layout.TableLayout;
import cz.possoft.client.gui.panel.nastavenispodnipanel.components.ExceptionPanel;
import cz.possoft.client.utils.PanelHolderUtils;
import cz.possoft.client.utils.SaveLoadUtils;
import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;
import cz.possoft.serializable.generic.Game;


/**
 *
 * @author mara
 */
public class UlozeniShowPanel extends JPanel {

    // list se seznamem drive ulozenych her
    private static JList listOfGames;
    
    // pole se seznamem her, kterer jsou zobrazeny v listu listOfGames
    private Vector vectorOfGames = new Vector();
    
    // scrollPane nad listem
    private static JScrollPane scrollPane;
    
    // tady bude jmeno souboru
    private static JTextField fileInputField;
    
    // tlacitko ktere zpusobi ulozeni
    private JButton saveButton;
    
    // tlacitko na mazani vybrane hry
    private JButton deleteButton;
    
    // spodni labely s hlaskama
    private static ExceptionPanel connectExceptionText;
    
     double[][] ulozeniSize = {{32, 60, 20, 29, 80, 32},   {32, 22, 10, 0, 10, 22, 178, 10, 22, 0, 44}};
    
    /** Creates a new instance of UlozeniShowPanel */
    public UlozeniShowPanel() {
         setLayout(new TableLayout(ulozeniSize));          
         initContents();
    }
    
    public void initContents() {        
        add(new JLabel(PropertiesManager.getInstance().getMessageForKey(Text.FILE)), "1,1");        
        add(getFileInputField(), "2,1,4,1");
//        connectLabel = new ConnectLabel(Text.NOTCONNECTED);
//        connectButton = new ConnectButton(Text.CONNECT);
//        add(connectButton, "1,3,2,3");
//        add(connectLabel, "3,3");
//        connectExceptionText = new ExceptionPanel("");
        
        //Object[] data = {"one", "<html><body><font color=blue>twoo</font></body></html>", "three", "four"};
        JLabel lab = new JLabel(PropertiesManager.getInstance().getMessageForKey(Text.SEZNAM_ULOZENYCH_SOUBORU));
        lab.setHorizontalAlignment(JLabel.LEFT);
        add(lab,"1,5,4,5");         
//        vyzivaciButton = new JButton(Text.VYZVAT);
//        vyzivaciButton.addActionListener(new VyzivaciListener());
//        vyzivaciButton.setEnabled(false);
        add(getSaveButton(), "1,8,2,8");
        add(getDeleteButton(), "4,8");
        getListOfGames();
        add(scrollPane, "1,6,4,6");
        //add(new JButton("cc"), "1,9,3,9");        
        add(getConnectExceptionText(), "1,10,5,10");
    }
    
    public JList getListOfGames() {
        if (listOfGames == null) {
            listOfGames = new JList();
            scrollPane = new JScrollPane(listOfGames);
            scrollPane.setAutoscrolls(false);
            listOfGames.addListSelectionListener(new MyListSelectionListener());
            refreshList();
        }
        return listOfGames;
    }
    
    public JButton getSaveButton() {
        if (saveButton == null) {
            saveButton = new JButton(PropertiesManager.getInstance().getMessageForKey(Text.SAVE_GAME_BUTTON));
            saveButton.addActionListener(new SaveListener());
        }
        return saveButton;
    }

    public static ExceptionPanel getConnectExceptionText() {
        if (connectExceptionText == null) {
            connectExceptionText = new ExceptionPanel("");
        }
        return connectExceptionText;
    }

    public static JTextField getFileInputField() {
        if (fileInputField == null) {
            fileInputField = new JTextField("");
        }
        return fileInputField;
    }

    public JButton getDeleteButton() {
        if (deleteButton == null) {
            deleteButton = new JButton(PropertiesManager.getInstance().getMessageForKey(Text.DELETE));
            deleteButton.addActionListener(new DeleteListener());
        }
        return deleteButton;
    }
    
    
    
    // refresne list s jiz ulozenyma hrama z adresare games
    private void refreshList() {
        vectorOfGames.clear();
        String[] p = SaveLoadUtils.getSavedGames();
        for (int i = 0 ; i<p.length; i++) {
            String s = p[i];        
            if (s.indexOf(".svg") == s.length() - 4) vectorOfGames.add(transformFileName(s));
        }        
        getListOfGames().setListData(vectorOfGames);
        getDeleteButton().setEnabled(false);
    }
    
    // transformuje jmeno souboru pro prijemne zobrazeni v listu
    private String transformFileName(String fileName) {
        String withoutSvg = fileName.substring(0, fileName.length() - 4);
        return "<HTML><BODY><FONT color=red>" + withoutSvg + "</FONT></BODY></HTML>";
    }
    
    private class SaveListener implements ActionListener {
    
        // nastava pri stisku tlacitka 'ulozit'
        public void actionPerformed(ActionEvent ae) {
            if (getFileInputField().getText().trim().length() == 0) {
                getConnectExceptionText().setText(PropertiesManager.getInstance().getMessageForKey(Text.ERROR_SAVE_NAME));
            }   
            else {
                Game game = PanelHolderUtils.getCurrentGamePanel().getGame();
                String opponentName = game.isMeWhite() ? game.getBlackPlayerName() : game.getWhitePlayerName();            
                int writeGameFlag = SaveLoadUtils.writeGame(game, getFileInputField().getText().trim(), opponentName);
                if (writeGameFlag == 0) {
                    getConnectExceptionText().setText(""); 
                    refreshList();
                }
                else if (writeGameFlag == 1) {
                   getConnectExceptionText().setText(PropertiesManager.getInstance().getMessageForKey(Text.ERROR_SAVE_FILE)); 
                }
                else if (writeGameFlag == 2) {
                   getConnectExceptionText().setText(PropertiesManager.getInstance().getMessageForKey(Text.ERROR_NAME_PRESENT)); 
                }
        }
    }
    }
    
    private class DeleteListener implements ActionListener {
        
        // nastava pri stisku tlacitka delete
        public void actionPerformed(ActionEvent ae) {
            String selectedGame = (String)getListOfGames().getSelectedValue();
            if (SaveLoadUtils.deleteSavedGame(selectedGame)) {
                getConnectExceptionText().setText(""); 
            }
            else {
                getConnectExceptionText().setText(PropertiesManager.getInstance().getMessageForKey(Text.ERROR_DELETE_FILE)); 
            }
            refreshList();
        }
    }
    
    // listener zabezpecuje disablovani a enablovani tlacitka na delete souperum.
    private class MyListSelectionListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent lse) {                            
                getDeleteButton().setEnabled(true);            
        }
    }
}
